package com.qyhl.school.school.reporter.clue;

import com.qyhl.school.common.SchoolUrl;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.PathConfigConstant;
import com.qyhl.webtv.commonlib.entity.UpTokenBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

public class SchoolReportClueModel implements SchoolReportClueContact.SchoolReportClueModel {
    private SchoolReportCluePresenter mPresenter;

    public SchoolReportClueModel(SchoolReportCluePresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void postClue(String username, String title, String content, String address, String phoneNum
            , double lng, double lat, String image) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(SchoolUrl.BASE_SCHOOL_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.SCHOOL_VERSION);
            object.put("name", "provideClue");
            object.put("username", username);
            object.put("title", title);
            object.put("address", address);
            object.put("phoneNum", phoneNum);
            object.put("lng", lng);
            object.put("lat", lat);
            object.put("content", content);
            object.put("image", image);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.postResult(false, e.getMessage());
                    }

                    @Override
                    public void onSuccess(String bean) {
                        mPresenter.postResult(true, "提交成功！");
                    }
                });
    }

    @Override
    public void getToken(boolean isUpload) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(SchoolUrl.BASE_SCHOOL_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.SCHOOL_VERSION);
            object.put("name", "getToken");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<UpTokenBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getTokenError(isUpload);
                    }

                    @Override
                    public void onSuccess(UpTokenBean bean) {
                        mPresenter.getTokenSuccess(bean, isUpload);
                    }
                });

    }
}
